import java.net.*;
import java.io.*;

public class TalkClient {

     public static void main( String ags[] ) {
	   Socket socket = null;
	   BufferedReader reader = null;
	   BufferedWriter writer = null;
	   Thread 	  agent  = null;
	   String	  msg    = null;
	   try {
		socket = new Socket ("sleipnir.cs.csubak.edu", 2001);
		agent = new Thread ( new Agent( socket ) );
	        writer = new BufferedWriter (
			new OutputStreamWriter( socket.getOutputStream() ) );
		agent.start();

	   } catch (IOException e) { }

	   while ( true ) {
	     try {
		  msg = ScreenIO.promptForString("Next Message: ");
		 writer.write( msg, 0, msg.length() );
	     } catch (IOException e) { } 
	   }
     }
}

class Agent implements Runnable {

   BufferedReader	reader = null;
   String		msg = null;
   Socket		socket = null;

   public Agent( Socket sk) {
	socket = sk; 
	try {
	    reader = new BufferedReader (
	 		new InputStreamReader(  socket.getInputStream () ) );
	} catch (IOException e ) {}
   }

   public void run() {
      System.out.printf("TalkClient is running ...\n");
      while ( true ) {
	 try {
             System.out.printf("Before reading from socket ...\n");
	     msg = reader.readLine();
	     System.out.printf("Message from Client: %s\n", msg);
	 } catch (IOException e) {}
      }
   }
}
